import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import static java.lang.System.exit;

public class AcademicManagementSystem {
    private Map<String, Teacher> teachers = new HashMap<>();
    private Map<String, Student> students = new HashMap<>();
    private Map<String, Course> courses = new HashMap<>();
    private Map<String, Classroom> classrooms = new HashMap<>();
    private ArrayList<Enrollment> enrollments = new ArrayList<>();
   private Map<String, Class> classes = new HashMap<>();
   private Map<String, stucoursemsg> stucoursemsg = new HashMap<>();


    private String dataFilePath = "D:/课设/jiaowuguanlixitong/";



    Scanner sc= new Scanner(System.in);

    public AcademicManagementSystem() {
        loadData();
    }

    private void loadData() {
        teachers = DataLoader.loadTeachers(dataFilePath);
        students = DataLoader.loadStudents(dataFilePath);
        courses = DataLoader.loadCourses(dataFilePath);
        classrooms = DataLoader.loadClassrooms(dataFilePath);
        enrollments = DataLoader.loadEnrollments(dataFilePath);
        classes = DataLoader.loadClass(dataFilePath);
        stucoursemsg = DataLoader.loadStucoursemsg(dataFilePath);

        // 加载数据的逻辑
    }

    private void saveData() {
        DataStorage.saveTeachers(teachers, dataFilePath);
        DataStorage.saveStudents(students, dataFilePath);
        DataStorage.saveCourses(courses, dataFilePath);
        DataStorage.saveClassrooms(classrooms, dataFilePath);
        DataStorage.saveEnrollments(enrollments, dataFilePath);
        DataStorage.saveClasses(classes, dataFilePath);
        DataStorage.saveStucoursemsg(stucoursemsg, dataFilePath);
    }

    public void showAdminMenu() {
        // 命令行模式下的管理员操作菜单
        System.out.println("管理员操作菜单：");
        System.out.println("1. 新增信息");
        System.out.println("2. 查看信息");
        System.out.println("3. 修改信息");
        System.out.println("4. 删除信息");
        System.out.println("5. 排课");
        System.out.println("6. 退出管理员界面");

        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addInfo();
                break;
            case 2:
                viewInfo();
                break;
            case 3:
                modifyInfo();
                break;
            case 4:
                deleteInfo();
                break;
            case 5:
                scheduleCourses();
                break;
            case 6:
                MainMenu();
            default:
                System.out.println("无效的选择，请重新输入");
        }
    }//管理员功能界面


    public void showStudentMenu() {
        // 命令行模式下的学生操作菜单
        System.out.println("学生操作菜单：");
        System.out.println("1. 个人信息管理");
        System.out.println("2. 教学反馈");
        System.out.println("3. 查询班级课表");
        System.out.println("4. 导出班级课表");
        System.out.println("5. 课程成绩查询");
        System.out.println("6. 退出学生界面");

        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                manageStudentInfo();
                break;
            case 2:
                giveFeedback();
                break;
            case 3:
                viewStudentSchedule();
                break;
            case 4:
                exportStudentSchedule();
                break;
            case 5:
                viewStudentGrade();
                break;
            case 6:
                MainMenu();
                break;
            default:
                System.out.println("无效的选择，请重新输入");
        }
    }//学生功能界面


    public void showTeacherMenu() {
        // 命令行模式下的教师操作菜单
        System.out.println("教师操作菜单：");
        System.out.println("1. 输入学生成绩");
        System.out.println("2. 查看学生反馈");
        System.out.println("3. 成绩排序");
        System.out.println("4. 查询本人课表");
        System.out.println("5. 导出班级课表");
        System.out.println("6. 退出");

        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                enterGrade();
                break;
            case 2:
                viewFeedback();
                break;
            case 3:
                sortGrade();
                break;
            case 4:
                viewTeacherSchedule();
                break;
            case 5:
                exportTeacherSchedule();
                break;
            case 6:
                MainMenu();
                break;
            default:
                System.out.println("无效的选择，请重新输入");
        }
    }//教师功能界面


    public void addInfo() {
        System.out.println("请选择要添加的信息类型（教师、学生、课程、教室、班级）：");
        String type = sc.next();
        while (true) {
            switch (type) {
                case "教师":
                    addTeacher();
                    break;
                case "学生":
                    addStudent();
                    break;
                case "课程":
                    addCourse();
                    break;
                case "教室":
                    addClassroom();
                    break;
                case "班级":
                    addClass();
                    break;
                default:
                    System.out.println("无效的信息类型，请重新输入");
            }

            saveData();
            break;
        }
    }//管理员添加信息功能界面


    public void addTeacher() {
        System.out.println("输入教师工号：");
        String id = sc.next();
        if (teachers.containsKey(id)) {
            System.out.println("教师已存在");
        } else {
            System.out.println("输入教师姓名：");
            String name = sc.next();
            System.out.println("输入教师性别：");
            String gender = sc.next();
            System.out.println("输入教师出生年月：");
            String birthDate = sc.next();
            System.out.println("输入教师电话：");
            String phone = sc.next();
            teachers.put(id, new Teacher(id, name, gender, birthDate, phone,0));
            System.out.println("教师添加成功");
        }
        saveData();
    }//管理员新增教师


    public void addStudent() {
        System.out.println("输入学生学号：");
        String id = sc.next();
        if (students.containsKey(id)) {
            System.out.println("学生已存在");
        } else {
            System.out.println("输入学生姓名：");
            String name = sc.next();
            System.out.println("输入学生性别：");
            String gender = sc.next();
            System.out.println("输入学生出生年月：");
            String birthDate = sc.next();
            System.out.println("输入学生电话：");
            String phone = sc.next();
            System.out.println("输入学生班级ID：");
            String classId = sc.next();
            if (!classes.containsKey(classId)) {
                System.out.println("班级ID不存在");
                return;
            }
            students.put(id, new Student(id, name, gender, birthDate, phone,classId));
            System.out.println("学生添加成功");
        }
        saveData();
    }//管理员新增学生


    public void addCourse() {
        System.out.println("输入课程号：");
        String courseId = sc.next();
        if (courses.containsKey(courseId)) {
            System.out.println("课程ID已存在");
        } else {
            System.out.println("输入课程名称：");
            String name = sc.next();
            System.out.println("输入课程学时：");
            int hours = sc.nextInt();
            System.out.println("输入课程学分：");
            double credits = sc.nextDouble();
            System.out.println("输入课程开始周：");
            String startWeek = sc.next();
            System.out.println("输入课程结束周：");
            String endWeek = sc.next();
            courses.put(courseId, new Course(courseId, name, hours, credits,
                            startWeek, endWeek, null, null, null));
            System.out.println("课程添加成功");
            saveData();
                }
            }
        //添加课程

    public void addClassroom() {
        System.out.println("输入教室编号：");
        String classroomId = sc.next();
        if (classrooms.containsKey(classroomId)) {
            System.out.println("教室已存在");
        } else {
            System.out.println("输入教室名称：");
            String name = sc.next();
            System.out.println("输入教室容量：");
            int capacity = sc.nextInt();
            classrooms.put(classroomId, new Classroom(classroomId, name, capacity));
            System.out.println("教室添加成功");
        }
        saveData();
    }//新增教室
    public void addClass() {
        System.out.println("输入班级ID：");
        String classId = sc.next();
        if (students.containsKey(classId)) {
            System.out.println("班级ID已存在");
        }
        else {
            System.out.println("输入班级名称：");
            String name = sc.next();
            classes.put(classId, new Class(classId, name));
            System.out.println("班级添加成功");
        }
        saveData();

    }

    public void viewInfo() {
        System.out.println("请选择要查看的信息类型（教师、学生、课程、教室）：");
        String type = sc.next();
        switch (type) {
            case "教师":
                viewTeachers();
                break;
            case "学生":
                viewStudents();
                break;
            case "课程":
                viewCourses();
                break;
            case "教室":
                viewClassrooms();
                break;
            default:
                System.out.println("无效的信息类型，请重新输入");
        }
    }//管理员查看信息功能界面

    public void viewTeachers() {
        for (Teacher teacher : teachers.values()) {
            System.out.println("教师工号：" + teacher.getId());
            System.out.println("教师姓名：" + teacher.getName());
            System.out.println("教师性别：" + teacher.getGender());
            System.out.println("教师出生年月：" + teacher.getBirthDate());
            System.out.println("教师电话：" + teacher.getPhone());
            System.out.println("------------------------");
        }
    }//管理员查看教师信息

    public void viewStudents() {
        for (Student student : students.values()) {
            System.out.println("学生学号：" + student.getId());
            System.out.println("学生姓名：" + student.getName());
            System.out.println("学生性别：" + student.getGender());
            System.out.println("学生出生年月：" + student.getBirthDate());
            System.out.println("学生电话：" + student.getPhone());
            System.out.println("------------------------");
        }
    }//管理员查看学生信息

    public void viewCourses() {
        for (Course course : courses.values()) {
            System.out.println("课程号：" + course.getCourseId());
            System.out.println("课程名称：" + course.getName());
            System.out.println("课程学时：" + course.getHours());
            System.out.println("课程学分：" + course.getCredits());
            System.out.println("课程开始周：" + course.getStartWeek());
            System.out.println("课程结束周：" + course.getEndWeek());
            System.out.println("课程上课周几：" + course.getDayOfWeek());
            System.out.println("课程第几节课：" + course.getClassPeriod());
            System.out.println("课程上课地点：" + course.getLocation());
            System.out.println("------------------------");
        }
    }//管理员查看课程信息

    public void viewClassrooms() {
        for (Classroom classroom : classrooms.values()) {
            System.out.println("教室编号：" + classroom.getClassroomId());
            System.out.println("教室名称：" + classroom.getName());
            System.out.println("教室容量：" + classroom.getCapacity());
            System.out.println("------------------------");
        }
    }//管理员查看教室信息

    public void modifyInfo() {
        System.out.println("请选择要修改的信息类型（教师、学生、课程、教室）：");
        String type = sc.next();
        switch (type) {
            case "教师":
                modifyTeacher();
                break;
            case "学生":
                modifyStudent();
                break;
            case "课程":
                modifyCourse();
                break;
            case "教室":
                modifyClassroom();
                break;
            default:
                System.out.println("无效的信息类型，请重新输入");
        }
        saveData();
    }//管理员修改信息功能界面

    public void modifyTeacher() {
        System.out.println("输入要修改的教师工号：");
        String id = sc.next();
        if (teachers.containsKey(id)) {
            System.out.println("输入新的教师姓名：");
            String name = sc.next();
            System.out.println("输入新的教师性别：");
            String gender = sc.next();
            System.out.println("输入新的教师出生年月：");
            String birthDate = sc.next();
            System.out.println("输入新的教师电话：");
            String phone = sc.next();
            teachers.put(id, new Teacher(id, name, gender, birthDate, phone,0));
            System.out.println("教师信息修改成功");
        } else {
            System.out.println("未找到该教师");
        }
    }//管理员修改教师信息

    public void modifyStudent() {
        System.out.println("输入要修改的学生学号：");
        String id = sc.next();
        if (students.containsKey(id)) {
            System.out.println("输入新的学生姓名：");
            String name = sc.next();
            System.out.println("输入新的学生性别：");
            String gender = sc.next();
            System.out.println("输入新的学生出生年月：");
            String birthDate = sc.next();
            System.out.println("输入新的学生电话：");
            String phone = sc.next();
            System.out.println("输入新的学生班级ID：");
            String classId = sc.next();
            if (!classes.containsKey(classId)) {
                System.out.println("班级ID不存在");
                return;
            }
            students.put(id, new Student(id, name, gender, birthDate, phone,classId));
            System.out.println("学生信息修改成功");
        } else {
            System.out.println("未找到该学生");
        }
    }//管理员修改学生信息

    public void modifyCourse() {
        System.out.println("输入要修改的课程号：");
        String courseId = sc.next();
        if (courses.containsKey(courseId)) {
            System.out.println("输入新的课程名称：");
            String name = sc.next();
            System.out.println("输入新的课程学时：");
            int hours = sc.nextInt();
            System.out.println("输入新的课程学分：");
            double credits = sc.nextDouble();
            System.out.println("输入新的课程开始周：");
            String startWeek = sc.next();
            System.out.println("输入新的课程结束周：");
            String endWeek =sc.next();
            System.out.println("输入新的课程上课周几：");
            String dayOfWeek = sc.next();
            System.out.println("输入新的课程第几节课：");
            String classPeriod = sc.next();
            System.out.println("输入新的课程上课地点：");
            String location = sc.next();
            for (Course course : courses.values()) {
                if (course.getDayOfWeek().equals(dayOfWeek) && course.getClassPeriod().equals(classPeriod)) {
                    System.out.println("该时间已经被占用，请重新输入");
                } else if (course.getLocation().equals(location)) {
                    System.out.println("该教室已经被占用，请重新输入");
                } else {
                    courses.put(courseId, new Course(courseId, name, hours, credits,
                            startWeek, endWeek, dayOfWeek, classPeriod, location));
                    System.out.println("课程信息修改成功");
                    saveData();
                }
            }
        } else {
            System.out.println("未找到该课程");
        }
    }//管理员修改课程信息

    public void modifyClassroom() {
        System.out.println("输入要修改的教室编号：");
        String classroomId = sc.next();
        if (classrooms.containsKey(classroomId)) {
            System.out.println("输入新的教室名称：");
            String name = sc.next();
            System.out.println("输入新的教室容量：");
            int capacity = sc.nextInt();
            classrooms.put(classroomId, new Classroom(classroomId, name, capacity));
            System.out.println("教室信息修改成功");
        } else {
            System.out.println("未找到该教室");
        }
    }//管理员修改教室信息

    public void deleteInfo() {
        System.out.println("请选择要删除的信息类型（教师、学生、课程、教室）：");
        String type = sc.next();
        switch (type) {
            case "教师":
                deleteTeacher();
                break;
            case "学生":
                deleteStudent();
                break;
            case "课程":
                deleteCourse();
                break;
            case "教室":
                deleteClassroom();
                break;
            default:
                System.out.println("无效的信息类型，请重新输入");
        }
        saveData();
    }//管理员删除信息功能界面

    public void deleteTeacher() {
        System.out.println("输入要删除的教师工号：");
        String id = sc.next();
        if (teachers.containsKey(id)) {
            teachers.remove(id);
            System.out.println("教师信息删除成功");
        } else {
            System.out.println("未找到该教师");
        }
    }//管理员删除教师信息

    public void deleteStudent() {
        System.out.println("输入要删除的学生学号：");
        String id = sc.next();
        if (students.containsKey(id)) {
            students.remove(id);
            System.out.println("学生信息删除成功");
        } else {
            System.out.println("未找到该学生");
        }
    }//管理员删除学生信息

    public void deleteCourse() {
        System.out.println("输入要删除的课程号：");
        String courseId = sc.next();
        if (courses.containsKey(courseId)) {
            courses.remove(courseId);
            System.out.println("课程信息删除成功");
        } else {
            System.out.println("未找到该课程");
        }
    }//管理员删除课程信息

    public void deleteClassroom() {
        System.out.println("输入要删除的教室编号：");
        String classroomId = sc.next();
        if (classrooms.containsKey(classroomId)) {
            classrooms.remove(classroomId);
            System.out.println("教室信息删除成功");
        } else {
            System.out.println("未找到该教室");
        }
    }//管理员删除教室信息

    public void scheduleCourses() {
        System.out.println("请选择排课方式（手动、自动）：");
        String way = sc.next();
        switch (way) {
            case "手动":
                manualSchedule();
                break;
            case "自动":
                autoSchedule();
                break;
            default:
                System.out.println("无效的排课方式，请重新输入");
        }
        saveData();
        // 实现排课的逻辑
    }//选择排课方式
    public void manualSchedule() {
        System.out.println("请输入课程号：");
        String courseId = sc.next();
        if (courses.containsKey(courseId)) {
            System.out.println("请输入教师工号：");
            String teacherId = sc.next();
            Teacher teacher = teachers.get(teacherId);
            if (teachers.containsKey(teacherId)&&teacher.getCouresnum()<3) {
                System.out.println("请输入开始周：");
                String startWeek = sc.next();
                System.out.println("请输入结束周：");
                String endWeek = sc.next();
                System.out.println("请输入上课周几：");
                String dayOfWeek = sc.next();
                System.out.println("请输入第几节课：");
                String classPeriod = sc.next();
                System.out.println("请输入教室编号：");
                String classroomId = sc.next();
                Classroom classroom = classrooms.get(classroomId);
                for (Course course : courses.values()) {
                    if (course.getDayOfWeek().equals(dayOfWeek) && course.getClassPeriod().equals(classPeriod)) {
                        System.out.println("该时间已经被占用，请重新输入");
                    } else if (course.getLocation().equals(classroom.getName())) {
                        System.out.println("该教室已经被占用，请重新输入");
                    } else {
                        courses.put(courseId, new Course(courseId, course.getName(), course.getHours(),
                                course.getCredits(),
                                startWeek, endWeek, dayOfWeek, classPeriod,classroom.getName()));
                        saveData();
                        System.out.println("课程排课成功");
                    }
                }
            }else {
                System.out.println("未找到该教师或该教师已排满课程");
            }
        }else {
            System.out.println("未找到该课程");
        }
    }//手动排课
    public void autoSchedule() {
        Random r = new Random();
        Map<String,Classroom> classrooms1 = new HashMap<>();
        Map<String,Classroom> classrooms2 = new HashMap<>();
        Map<String,Classroom> classrooms3 = new HashMap<>();
        Map<String,Classroom> classrooms4 = new HashMap<>();
        Map<String,Classroom> classrooms5 = new HashMap<>();
        Map<String,Classroom> classrooms6 = new HashMap<>();
        Map<String,Classroom> classrooms7 = new HashMap<>();
        classrooms1.putAll(classrooms);
        classrooms2.putAll(classrooms);
        classrooms3.putAll(classrooms);
        classrooms4.putAll(classrooms);
        classrooms5.putAll(classrooms);
        classrooms6.putAll(classrooms);
        classrooms7.putAll(classrooms);
         //排课的时间与地点
        for (Course course : courses.values()) {
           Course course1 = courses.get(course.getCourseId());
            int i = r.nextInt(84);
            int m = i+1;
            int d = m%13;
            String s = String.valueOf(d);
            //判断星期几
            if (m>=1&&m<=12) {
                String[] keys = classrooms1.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms1.get(randomKey).getName();
                course1.setDayOfWeek("周一");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms1.remove(randomKey);
            }else if (m>=14&&m<=25) {
                String[] keys = classrooms2.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms2.get(randomKey).getName();
                course1.setDayOfWeek("周二");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms2.remove(randomKey);
            }
            else if (m>=27&&m<=38) {
                String[] keys = classrooms3.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms3.get(randomKey).getName();
                course1.setDayOfWeek("周三");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms3.remove(randomKey);
            }
            else if (m>=40&&m<=51) {
                String[] keys = classrooms4.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms4.get(randomKey).getName();
                course1.setDayOfWeek("周四");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms4.remove(randomKey);
            }
            else if (m>=53&&m<=64) {
                String[] keys = classrooms5.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms5.get(randomKey).getName();
                course1.setDayOfWeek("周五");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms5.remove(randomKey);
            }
            else if (m>=66&&m<=77) {
                String[] keys = classrooms6.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms6.get(randomKey).getName();
                course1.setDayOfWeek("周六");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
            }
            else if (m>=79&&m<=84) {
                String[] keys = classrooms7.keySet().toArray(new String[0]);
                int randomIndex = r.nextInt(keys.length);
                String randomKey = keys[randomIndex];
                String Location = classrooms7.get(randomKey).getName();
                course1.setDayOfWeek("周日");
                course1.setClassPeriod(s);
                course1.setLocation(Location);
                classrooms7.remove(randomKey);
            }
            courses.put(course1.getCourseId(),course1);

             //将课程平均分配给班级
            int cous = classes.size();
            int cs = courses.size();
            int coursesPerClass = cs / cous;
            int remainder = cs % cous;
            String[] courseIds = courses.keySet().toArray(new String[0]);
            int index = 0;
            for(Class aClass : classes.values()) {
               for (int j = 0; j < coursesPerClass; j++) {
                    stucoursemsg.put(courseIds[index], new stucoursemsg(courseIds[index],aClass.getClassid(),null));
                    index++;
                }
               if (remainder > 0) {
                    stucoursemsg.put(courseIds[index], new stucoursemsg(courseIds[index],aClass.getClassid(),null));
                    index++;
                    remainder--;
               }
            }

            Map<String,Teacher> teachers1 = new HashMap<>();
            teachers1.putAll(teachers);
            //将老师随机分配给班级
            for(stucoursemsg stucoursemsg : stucoursemsg.values()) {
                String[] teacherIds = teachers1.keySet().toArray(new String[0]);
                if (teacherIds.length > 0) {
                    int randomIndex = r.nextInt(teacherIds.length);
                    String randomKey = teacherIds[randomIndex];
                    stucoursemsg.setTeacherID(randomKey);
                    Teacher teacher = teachers.get(randomKey);
                    teachers.put(randomKey,teacher);
                    if (teacher.getCouresnum()>2) {
                        teachers1.remove(randomKey);
                    }
                }
            }
            enrollments.clear();
            for(stucoursemsg stucoursemsg : stucoursemsg.values()) {
                Course cou = courses.get(stucoursemsg.getCourseId());
              for(Student student : students.values()) {
                  if (student.getClassid().equals(stucoursemsg.getClassID())) {
                      enrollments.add(new Enrollment(stucoursemsg.getCourseId(),stucoursemsg.getTeacherID(),student.getId(),0,null,cou.getCourseId()));
                  }
              }
            }
        }
        saveData();
        System.out.println("课程排课成功");
        // 实现自动排课的逻辑
    }//自动排课

    public void manageStudentInfo() {
        System.out.println("请输入你的学号：");
        String studentId = sc.next();
        if (students.containsKey(studentId)) {
            Student student = students.get(studentId);
            String name = student.getName();
            String gender = student.getGender();
            System.out.println("输入新的学生出生年月：");
            String birthDate = sc.next();
            System.out.println("输入新的学生电话：");
            String phone = sc.next();
            students.put(studentId, new Student(studentId, name, gender, birthDate, phone, student.getClassid()));
            saveData();
            System.out.println("学生信息修改成功");
        } else {
            System.out.println("未找到该学生");
        }
    }//学生个人信息管理

    public void giveFeedback() {
        System.out.println("请输入你的学号：");
        String studentId =sc.next();
        if (students.containsKey(studentId)) {
            System.out.println("请输入课程号：");
            String courseId = sc.next();
            if (courses.containsKey(courseId)) {
                System.out.println("请输入反馈内容：");
                String feedback = sc.next();
                for (Enrollment enrollment : enrollments) {
                    if (enrollment.getStudentId().equals(studentId) && enrollment.getCourseId().equals(courseId)) {
                        enrollment.setFeedback(feedback);
                        System.out.println("反馈成功");
                        saveData();
                    }
                }
            }
        }
    }//学生提供反馈
    public void viewStudentSchedule() {
        System.out.println("请输入你的学号：");
        String studentId = sc.next();
        Set<String> courseIds = new HashSet<>();
        if (students.containsKey(studentId)) {
            System.out.println("查询课表成功！");
            for (Enrollment enrollment : enrollments) {
                if (enrollment.getStudentId().equals(studentId)&&!courseIds.contains(enrollment.getCourseId())) {
                    String courseId = enrollment.getCourseId();
                    Course course = courses.get(courseId);
                    System.out.println("课程号：" + course.getCourseId());
                    System.out.println("课程名称：" + course.getName());
                    System.out.println("课程开始周：" + course.getStartWeek());
                    System.out.println("课程结束周：" + course.getEndWeek());
                    System.out.println("课程上课周几：" + course.getDayOfWeek());
                    System.out.println("课程第几节课：" + course.getClassPeriod());
                    System.out.println("课程上课地点：" + course.getLocation());
                    System.out.println("------------------------");
                    courseIds.add(enrollment.getCourseId());
                }
            }
        }else {
            System.out.println("未找到该学生");
        }
    }//学生查看课表
    public void exportStudentSchedule() {
        /*
        将课程导入为文本文件
         */
        System.out.println("请输入你的学号：");
        String studentId = sc.next();
        Set<String> courseIds = new HashSet<>();
                    try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataFilePath+studentId + "schedule.txt"))) {
                       for(Enrollment enrollment : enrollments) {
                            if (enrollment.getStudentId().equals(studentId)&&!courseIds.contains(enrollment.getCourseId())) {
                                Course course = courses.get(enrollment.getCourseId());
                                if (course!= null) {
                                    writer.write(course.getName() +"在"+course.getLocation()+ " ，时间为 " + course.getDayOfWeek() + " 的第 " + course.getClassPeriod() + " 节，从第" + course.getStartWeek() + "周到" + course.getEndWeek()+"周");
                                    writer.newLine();
                                }
                                courseIds.add(enrollment.getCourseId());
                            }
                        }
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
            System.out.println("课表导出成功，文件在：" +"D:\\课设\\jiaowuguanlixitong"+ studentId + "_schedule.txt");
        }
    //学生导出课表
    public void viewStudentGrade() {
        System.out.println("请输入你的学号：");
        String studentId = sc.next();
        if (students.containsKey(studentId)) {
            for (Enrollment enrollment : enrollments) {
                if (enrollment.getStudentId().equals(studentId)) {
                    String courseId = enrollment.getCourseId();
                    Course course = courses.get(courseId);
                    System.out.println("课程名称：" + course.getCourseId());
                    System.out.println("课程名称：" + course.getName());
                    System.out.println("课程成绩：" + enrollment.getGrade());
                    System.out.println("------------------------");
                }
            }
        }
        else {
            System.out.println("未找到该学生");
        }
    }//学生查看成绩

    public void enterGrade() {
        System.out.println("请输入你的工号：");
        String teacherId = sc.next();
        if (teachers.containsKey(teacherId)) {
            System.out.println("请输入学生学号：");
            String studentId =sc.next();
            if (students.containsKey(studentId)) {
                System.out.println("请输入课程号：");
                String courseId = sc.next();
                if (courses.containsKey(courseId)) {
                    System.out.println("请输入成绩：");
                    double grade = sc.nextDouble();
                    for (Enrollment enrollment : enrollments) {
                        if (enrollment.getStudentId().equals(studentId) && enrollment.getCourseId().equals(courseId)
                                &&enrollment.getTeacherId().equals(teacherId)) {
                            enrollment.setGrade(grade);
                            System.out.println("成绩录入成功");
                            saveData();
                        }
                    }
                }else {
                    System.out.println("未找到该课程");
                }
            }else {
                System.out.println("未找到该学生");
            }
        }else {
            System.out.println("未找到该教师");
        }
    }//老师给分数
    public void viewFeedback() {
        System.out.println("请输入你的工号：");
        String teacherId = sc.next();
        if (teachers.containsKey(teacherId)) {
            for (Enrollment enrollment : enrollments) {
                if (enrollment.getTeacherId().equals(teacherId)) {
                    String studentId = enrollment.getStudentId();
                    Student student = students.get(studentId);
                    Course course = courses.get(enrollment.getCourseId());
                    System.out.println("课程名称：" + course.getName());
                    System.out.println("学生姓名：" + student.getName());
                    System.out.println("学生学号：" + studentId);
                    System.out.println("反馈内容：" + enrollment.getFeedback());
                    System.out.println("------------------------");
                }
            }
        }else {
            System.out.println("未找到该教师");
        }
    }//老师查看反馈
    public void sortGrade() {
        System.out.println("请输入课程号：");
        String courseId = sc.next();
        if (courses.containsKey(courseId)) {
            List<Enrollment> sortedEnrollments = new ArrayList<>(enrollments);
            sortedEnrollments.sort(Comparator.comparingDouble(Enrollment::getGrade).reversed());
            for (Enrollment enrollment : sortedEnrollments) {
                if (enrollment.getCourseId().equals(courseId)) {
                    String studentId = enrollment.getStudentId();
                    Student student = students.get(studentId);
                    System.out.println("学生姓名：" + student.getName());
                    System.out.println("学生学号：" + studentId);
                    System.out.println("成绩：" + enrollment.getGrade());
                    System.out.println("------------------------");
                }
            }
        }else {
            System.out.println("未找到该课程");
        }
    }//老师整理分数
    public void viewTeacherSchedule() {
        System.out.println("请输入你的工号：");
        String teacherId = sc.next();
        Set<String> courseIds = new HashSet<>();
        if (teachers.containsKey(teacherId)) {
            for (Enrollment enrollment : enrollments) {
                if (enrollment.getTeacherId().equals(teacherId)&&!courseIds.contains(enrollment.getCourseId())) {
                    String courseId = enrollment.getCourseId();
                    Course course = courses.get(courseId);
                    System.out.println("课程号：" + course.getCourseId());
                    System.out.println("课程名称：" + course.getName());
                    System.out.println("课程开始周：" + course.getStartWeek());
                    System.out.println("课程结束周：" + course.getEndWeek());
                    System.out.println("课程上课周几：" + course.getDayOfWeek());
                    System.out.println("课程第几节课：" + course.getClassPeriod());
                    System.out.println("课程上课地点：" + course.getLocation());
                    System.out.println("------------------------");
                    courseIds.add(courseId);
                }
            }
        }
    }//老师查看课表

    public void exportTeacherSchedule() {
        System.out.println("请输入你的工号：");
        String teacherId = sc.next();
        Set<String> courseIds = new HashSet<>();
        if (teachers.containsKey(teacherId)) {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataFilePath+teacherId + "schedule.txt"))) {
                for (Enrollment enrollment : enrollments) {
                    if (enrollment.getTeacherId().equals(teacherId)&&!courseIds.contains(enrollment.getCourseId())) {
                        Course course = courses.get(enrollment.getCourseId());
                        if (course!= null) {
                            writer.write(course.getName() +"在"+course.getLocation()+
                                    "时间为" + course.getDayOfWeek() + " 的第 " + course.getClassPeriod() +
                                    " 节，从第" + course.getStartWeek() + "周到第" + course.getEndWeek()+"周");
                            writer.newLine();
                        }
                        courseIds.add(enrollment.getCourseId());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("课表导出成功，文件在：" +"D:\\课设\\jiaowuguanlixitong"+ teacherId + "_schedule.txt");
        }
    }//老师导出课表
    public void MainMenu(){
        Scanner sc = new Scanner(System.in);
        System.out.println("***************欢迎来到教务管理系统，请输入您的身份进入系统***************");
        System.out.println("****                          1.教师                                ****");
        System.out.println("****                          2.学生                                ****");
        System.out.println("****                          3.管理员                              ****");
        System.out.println("****                          4.退出                                ****");
        System.out.println("**********************************************************************");
        System.out.println("请输入您的身份(填写序号)或退出：");
        int choice = sc.nextInt();
        AcademicManagementSystem academicManagementSystem = new AcademicManagementSystem();
        while (true) {
            switch (choice) {
                case 1:
                    academicManagementSystem.showTeacherMenu();
                    break;
                case 2:
                    academicManagementSystem.showStudentMenu();
                    break;
                case 3:
                    academicManagementSystem.showAdminMenu();
                    break;
                case 4:
                    System.out.println("退出成功");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }

        }
    }
}